package bs.app.Client;

import android.os.Build;
import bs.app.Constant.CConfig;
import bs.app.Constant.KeyConstant;
import bs.app.HTTPConnection.CConnection;
import bs.app.Main.CCanvas;
import bs.app.Position.CPosition;
import bs.app.Res.CRess;
import bs.app.android.Graphics;
import bs.app.android.Image;

public class AppPage2 {
	// 80,79,79
	public int xstartBackground, ystartBackground, xtextborder, ytextborder,xtexticon, ytexticon;
	public int xstartLine, ystartLine, distanceLine;
	public int xstartchosensub, ystartchosensub;
	public ItemApp[] item;
	public int indexFocus;
	public int elementItem;
	public int Cry, AmDuong;
	public boolean PressOnce = false;
	public final int NumberDivided = 2;
	public int LowerLimit;
	public int NumberPage = 1;
	public int TotalPage;
	public int Page;
	public int NumberRecordinPage;
	public int DistanceRecordinPage;
	public int perpage = 10; //6



	public AppPage2(int element) {
	}

	public void init(int element, String[][] data, Image[] img) {
		CCanvas.s_Instan.connect.isPainting = false;
		resetData();
		elementItem = element;
		CRess.LoadImagePage2(img);
		xstartBackground = xtextborder = xstartLine = xstartchosensub = 0;
		ystartBackground = 0;
		ytextborder = ystartBackground + 25;
		xtexticon = xstartBackground + 4;
		ytexticon = ystartBackground + 28;
		
		if("GT-S5360".equals(Build.MODEL))
		{
			ytexticon = ytexticon - 3; 
		}
		ystartLine = CCanvas.yTranslate + ytextborder + CRess.textborder_left.getHeight();
		ystartchosensub = ystartLine;

		
		// //////// can sua
		//System.out.println("Page:"+ Page);
		if (Page == 2 || Page == 4 || Page == 6) {			
			DistanceRecordinPage = 55; // 50
//			NumberRecordinPage = 5;		
		} else if (Page == 3) {
			DistanceRecordinPage = 67; // 36
//			NumberRecordinPage = 4; // 4			
		}
//		distanceLine = CRess.imgFocus.getHeight() - DistanceRecordinPage;
		distanceLine = DistanceRecordinPage;
		NumberRecordinPage  = (CCanvas.s_iHcanvas-ystartLine)/distanceLine;
		//System.out.println("NumberRecordinPage:"+ NumberRecordinPage);
		LowerLimit = ystartLine + distanceLine * NumberRecordinPage - CConfig.HIEGHT_COMMNAND;
		//System.out.println("LowerLimit:"+ LowerLimit);
		
		TotalPage = CCanvas.s_Instan.connect.TotalRecord / perpage;
		if (CCanvas.s_Instan.connect.TotalRecord % perpage != 0 && CCanvas.s_Instan.connect.TotalRecord > perpage) {
			TotalPage++;
		}

	
		CreateItem(element, data);
	}

	public void CreateItem(int element, String[][] data) {
		item = new ItemApp[element];
		for (int i = 0; i < element; i++) {
			item[i] = new ItemApp();
			item[i].row = i;
			item[i].colum = 0;
			item[i].indexcolor = 0;
			item[i].indexborder = 0;
			item[i].NumberPage = 2;
			item[i].x = CCanvas.xTranslate + 6;
			item[i].xFocus = 6;
			item[i].distanceRegion = distanceLine;
			item[i].xLine = CCanvas.xTranslate;
			item[i].yLine = ystartLine + i * distanceLine;
			item[i].h = distanceLine;
			item[i].w =CCanvas.s_iWcanvas;
			item[i].yFocus = 8;
			if (data[i][0] != null && !data[i][0].trim().equals(""))
			{
				item[i].Name = data[i][0].trim();
				System.out.println("name " + i + " :"  + item[i].Name );
			}
			if (data[i][1] != null && !data[i][1].trim().equals(""))
			{
				item[i].Link = data[i][1].trim();
				System.out.println("name " + i + " :"  + item[i].Link );
			
			}
			item[i].y = ystartLine + 8 + i * distanceLine;
			if (Page == 2) {
				item[i].xName = CRess.IconApp[i][0].getWidth() + 15;
				if (data[i][3] != null && !data[i][3].trim().equals("")) {
					item[i].Description = data[i][3];
					item[i].xDescription = item[i].xName + 14;
					item[i].yDescription = item[i].y + 11;
					item[i].yName = -3;
				} else {
					item[i].yName = 10;
					item[i].yImage = 6;
				}
			} else if (Page == 3) {
				item[i].Description = data[i][3];
				item[i].Date = data[i][4];
				item[i].xName = 45;
				item[i].yName = -3; //-6
				item[i].xDate = CCanvas.xTranslate + item[i].xName + 5;
				item[i].yDate = item[i].y + 12; //+9
				item[i].xDescription = CCanvas.xTranslate + item[i].xName + 5;
				item[i].yDescription = item[i].yDate + 15;
			} else if (Page == 4 || Page == 6) {
				if(!data[i][5].trim().equals("") && !data[i][5].trim().equals("0")){
					item[i].Price = "Giá tiền : " + data[i][5].trim() + " đ";
				}else{
					item[i].Price = "";
				}
				if (Page == 6)
					item[i].DownLoad = "Cài đặt";
				else
					item[i].DownLoad = "Download";
				String[] rating = CConnection.split(data[i][6].trim(), "\r\n");
				item[i].sorating = Integer.parseInt(rating[0].equals("") ? "0": rating[0]);
				item[i].xName = 48;
				item[i].yName = -1;
				item[i].xPrice = CCanvas.xTranslate + 51;
				item[i].yPrice = item[i].y - 8;
			}
					
		}
		item[0].isFocus = true;
		item[0].indexcolor = 1;
		item[0].indexborder = 1;
		
		
	}

	public void Paint(Graphics g) {
		paintBackGround(g);
		painttextborder(g);
		painttexticon(g);
		g.setClip(0, CConfig.HIEGHT_SUB_TITLE, CCanvas.s_iWcanvas, CCanvas.s_iHcanvas - CConfig.HIEGHT_COMMNAND -CConfig.HIEGHT_SUB_TITLE);
		for (int i = 0; i < item.length; i++) {
			if (item[i] != null) {
//				if (CCanvas.s_Instan.ispaint(g.getClipX(), g.getClipY(), g.getClipWidth(), g.getClipHeight() - 2, item[i].xLine,item[i].yLine, item[i].w, item[i].h)) 
				{
					item[i].Paint(g);
				}
			}
		}
		g.setClip(0, 0, CCanvas.s_iWcanvas, CCanvas.s_iHcanvas);
//		paintMenuBar(g);
		CCanvas.mediumfont.drawString(g, "Menu", 5,CCanvas.s_iHcanvas - CConfig.FONT_HEIGHT_COMMAND, 0, 0xa1a6a1, 0x090b7a, true);
		CCanvas.mediumfont.drawString(g, "Quay Lại",  CCanvas.s_iWcanvas - CCanvas.mediumfont.stringWidth("Quay Lại") - 3,CCanvas.s_iHcanvas - CConfig.FONT_HEIGHT_COMMAND, 0, 0xa1a6a1, 0x090b7a, true);
		
		if (TotalPage > 0) {
			CCanvas.mediumfont.drawString(g,"<" + NumberPage + "/" + TotalPage + ">", CCanvas.s_iWcanvas/ 2	- CCanvas.mediumfont.stringWidth("<" + NumberPage+ "//" + TotalPage + ">") / 2,
					CCanvas.s_iHcanvas - CConfig.FONT_HEIGHT_COMMAND, 0,0xa1a6a1, 0x090b7a, true);
		}
		CCanvas.s_Instan.connect.isPainting = true;

	}

	public void painttextborder(Graphics g) {
		g.drawImage(CRess.textborder_left, CCanvas.xTranslate + xtextborder, CCanvas.yTranslate + ytextborder, CPosition.ANCHOR_LEFT_TOP);
		int i=CRess.textborder_left.getWidth();
		for(; i< CCanvas.s_iWcanvas- CRess.textborder_right.getWidth();i+=CRess.textborder_midle.getWidth())
			g.drawImage(CRess.textborder_midle, CCanvas.xTranslate + xtextborder + i, CCanvas.yTranslate + ytextborder, CPosition.ANCHOR_LEFT_TOP);
		g.drawImage(CRess.textborder_right, CCanvas.s_iWcanvas-1, CCanvas.yTranslate + ytextborder, CPosition.ANCHOR_RIGHT_TOP);
	}

	public void paintBackGround(Graphics g) {
		g.setColor(0x000000);
		g.fillRect(0, 0, CCanvas.s_iWcanvas, CCanvas.s_iHcanvas);
		g.drawImage(CRess.background, CCanvas.s_iWcanvas>>1, CCanvas.s_iHcanvas>>1, CPosition.ANCHOR_HCENTER_VCENTER);
		if(CCanvas.s_iWcanvas > CRess.imgTitle.getWidth()){
			g.drawImage(CRess.imgTitle, 0, 0, CPosition.ANCHOR_LEFT_TOP);
			for(int i = CRess.imgTitle.getWidth(); i < CCanvas.s_iWcanvas + CRess.imgBanner_back.getWidth(); i+=CRess.imgBanner_back.getWidth())
				g.drawImage(CRess.imgBanner_back, i, 0, CPosition.ANCHOR_LEFT_TOP);

		}else{
			g.drawImage(CRess.imgTitle, 0, 0, CPosition.ANCHOR_LEFT_TOP);
		}
		for(int i=0; i < CRess.imgDotted.length; i++)
			g.drawImage(CRess.imgDotted[i], CCanvas.s_iWcanvas - 30 + (i * (CRess.imgDotted[i].getWidth()+5)) , 10, CPosition.ANCHOR_LEFT_TOP);
		paintMenuBar(g);
	}
	public void paintMenuBar(Graphics g){
		if(CCanvas.s_iWcanvas > CRess.menubar.getWidth()){
			g.drawImage(CRess.menubar, 0, CCanvas.s_iHcanvas, CPosition.ANCHOR_LEFT_BOTTOM);
			for(int i = CRess.menubar.getWidth(); i < CCanvas.s_iWcanvas + CRess.footer_back.getWidth(); i+=CRess.footer_back.getWidth())
				g.drawImage(CRess.footer_back, i, CCanvas.s_iHcanvas, CPosition.ANCHOR_LEFT_BOTTOM);

		}else{
			g.drawImage(CRess.menubar, 0, CCanvas.s_iHcanvas, CPosition.ANCHOR_LEFT_BOTTOM);

		}
	}

	public void painttexticon(Graphics g) {
		int vitri = xtexticon + CRess.texticon.getWidth() + 3;
		
		if("GT-S5360".equals(Build.MODEL))
			CCanvas.smallfont.drawString(g,(String) CCanvas.s_Instan.connect.Link.elementAt(0),CCanvas.xTranslate + vitri, CCanvas.yTranslate + ytexticon, 0, 0xf1941c, 0x201404, true);
		else 
			CCanvas.mediumfont.drawString(g,(String) CCanvas.s_Instan.connect.Link.elementAt(0),CCanvas.xTranslate + vitri, CCanvas.yTranslate + ytexticon, 0, 0xf1941c, 0x201404, true);
		vitri++;
		g.drawImage(CRess.texticon, CCanvas.xTranslate + xtexticon,
				CCanvas.yTranslate + ytexticon, CPosition.ANCHOR_LEFT_TOP);
		for (int i = 1; i < CCanvas.s_Instan.connect.Link.size(); i++) {
			vitri += CCanvas.smallfont.stringWidth((String) CCanvas.s_Instan.connect.Link.elementAt(i - 1)) + 5;
			g.drawImage(CRess.smallrow, CCanvas.xTranslate + vitri, CCanvas.yTranslate + ytexticon + 5, Graphics.TOP | Graphics.LEFT);
			vitri += CRess.smallrow.getWidth() + 2;
			String temp = "";
			if (vitri+ CCanvas.smallfont .stringWidth((String) CCanvas.s_Instan.connect.Link.elementAt(i))+ 5 > CCanvas.s_iWcanvas - 15) {
				String value = (String) CCanvas.s_Instan.connect.Link.elementAt(i);
				for (int j = 0; j < value.length(); j++) {
					if (vitri + CCanvas.smallfont.stringWidth(temp) + 5 > CCanvas.s_iWcanvas - 15) {
						temp = temp + "....";
					} else {
						temp += value.charAt(j);
					}
				}
				
				if("GT-S5360".equals(Build.MODEL))
					CCanvas.smallfont.drawString(g, temp, CCanvas.xTranslate + vitri, CCanvas.yTranslate + ytexticon, 0, 0xf1941c, 0x201404, true);
				else
					CCanvas.mediumfont.drawString(g, temp, CCanvas.xTranslate + vitri, CCanvas.yTranslate + ytexticon, 0, 0xf1941c, 0x201404, true);
			
			} else {
				
				if("GT-S5360".equals(Build.MODEL))
				CCanvas.smallfont.drawString(g, (String) CCanvas.s_Instan.connect.Link.elementAt(i), CCanvas.xTranslate + vitri,
						CCanvas.yTranslate + ytexticon, 0, 0xf1941c, 0x201404, true);
				else
				CCanvas.mediumfont.drawString(g, (String) CCanvas.s_Instan.connect.Link.elementAt(i), CCanvas.xTranslate + vitri,
						CCanvas.yTranslate + ytexticon, 0, 0xf1941c, 0x201404, true);
			}
		}
	}

	public void paintchosensub(Graphics g) {
		g.drawImage(CRess.imgFocus, xstartchosensub, ystartchosensub,
				CPosition.ANCHOR_LEFT_TOP);
	}

	static int iNewIndex = 0;
	static int iOldIndex = 0;

	public int pointerItems(int x, int y) {
		iOldIndex = indexFocus;
		int x0 = 0;
		int x1 =  CCanvas.s_iWcanvas;
		int y0 = CConfig.HIEGHT_TITLE;
		int y1 = CCanvas.s_iHcanvas - CConfig.HIEGHT_COMMNAND;
		//System.out.println("x:"+ x + " y:"+ y);
		if (x >= x0 && x <= x1 && y >= y0 && y <= y1) {
			for (int i = 0; i < item.length; i++) {
				if (item[i] != null) {					
					int yy0 = item[i].y;
					if (y >= yy0 && y < yy0 + DistanceRecordinPage) {
						if (indexFocus == i) { // fire
							CCanvas.keyPressed[KeyConstant.KEY_FIRE] = true;
							return 0;
						}
						item[indexFocus].isFocus = false;
						item[indexFocus].indexcolor = 0;
						item[indexFocus].indexborder = 0;
						// scrolling
						if (i > indexFocus) {
							indexFocus = i;							
							DetectScrollItem(1);

						} else if (i < indexFocus) {
							indexFocus = i;	
							DetectScrollItem(0);
						}
						//System.out.println("indexFocus:" + indexFocus);
						item[indexFocus].isFocus = true;
						item[indexFocus].indexcolor = 1;
						item[indexFocus].indexborder = 1;
						return 0;

					}
				}
			}			
		}
		return -1;
	}

	public void pointerPressed(int px, int py) {
		if (CCanvas.isPointerPressed) // User PointerPressed
		{
			if (Pointer.SelectSoftKey(px, py) == 0) { // left softkey
				CCanvas.keyPressed[KeyConstant.KEY_LEFT_SOFTKEY] = true;
			} else if (Pointer.SelectSoftKey(px, py) == 3) {
				CCanvas.keyPressed[KeyConstant.KEY_RIGHT_SOFTKEY] = true;
			} else if (Pointer.SelectSoftKey(px, py) == 1) {
				CCanvas.keyPressed[KeyConstant.KEY_LEFT] = true;
			} else if (Pointer.SelectSoftKey(px, py) == 2) {
				CCanvas.keyPressed[KeyConstant.KEY_RIGHT] = true;
			} else { // click into items
				pointerItems(px, py);
			}
		}
		CCanvas.resetPointerPress(); // User PointerReleased
	}

	public void UpdateKeyPress(boolean[] keyPressed, boolean[] keyHold,
			int currentAsccii[]) {
		if (CCanvas.s_Instan.connect.isPainting == true) {
			update();
			if (keyPressed[KeyConstant.KEY_RIGHT]) // User PressKey Fire
			{
				if (NumberPage < TotalPage) {
					CCanvas.s_Instan.LoadingState.timeForLoading = 50;
					CCanvas.s_Instan.LoadingState.time = CCanvas.s_Instan.LoadingState.fixtime;
					CCanvas.CurrentScreen = CCanvas.isChangeLoadingState;
					resetData();
					NumberPage++;
					String link = (String) CCanvas.s_Instan.connect.URL
							.lastElement();
					int begin = link.indexOf("&page=");
					int numberPageURL = 0;
					if (begin > 0) {
						int iBeginPerpage = link.indexOf("&perpage");
						numberPageURL = Integer.parseInt(link.substring(
								begin + 6, iBeginPerpage).trim());
						link = link.substring(0, begin);
					}
					CCanvas.s_Instan.connect.setURL(link + "&page="
							+ NumberPage + "&perpage=" + perpage);
					if (NumberPage > numberPageURL) {
						CCanvas.s_Instan.connect.numberpage = NumberPage;
						CCanvas.s_Instan.connect.URL.addElement(link + "&page="
								+ NumberPage + "&perpage=" + perpage);
					}
					CCanvas.s_Instan.connect.RequestServerGetData();
				}
			} else if (keyPressed[KeyConstant.KEY_LEFT]) // User PressKey Fire
			{

				if (NumberPage > 1) {
					CCanvas.s_Instan.LoadingState.timeForLoading = 50;
					CCanvas.s_Instan.LoadingState.time = CCanvas.s_Instan.LoadingState.fixtime;
					CCanvas.CurrentScreen = CCanvas.isChangeLoadingState;
					resetData();
					NumberPage--;
					if (CCanvas.s_Instan.connect.URL.size() > 0)
						CCanvas.s_Instan.connect.URL
								.removeElementAt(CCanvas.s_Instan.connect.URL
										.size() - 1);

					String link = (String) CCanvas.s_Instan.connect.URL
							.lastElement();
					int begin = link.indexOf("&page=");
					if (begin > 0) {
						link = link.substring(0, begin);
					}
					CCanvas.s_Instan.connect.numberpage = NumberPage;
					CCanvas.s_Instan.connect.setURL(link + "&page="
							+ NumberPage + "&perpage=" + perpage);
					CCanvas.s_Instan.connect.RequestServerGetData();
				}

			} else if (keyPressed[KeyConstant.KEY_UP]) // User PressKey Fire
			{
				SwapFocus(0);
			} else if (keyPressed[KeyConstant.KEY_DOWN]) // User PressKey Fire
			{
				SwapFocus(1);
			} else if (keyPressed[KeyConstant.KEY_FIRE]) // User PressKey Fire
			{
				if (item[indexFocus].isFocus == true) {
					if (Page == 2) {
						CCanvas.s_Instan.connect.koclearurl = -1;
						CCanvas.s_Instan.LoadingState.timeForLoading = 50;
						CCanvas.s_Instan.LoadingState.time = CCanvas.s_Instan.LoadingState.fixtime;
						CCanvas.CurrentScreen = CCanvas.isChangeLoadingState;
						NumberPage = 1;
						String sName = item[indexFocus].Link;
						if (sName.indexOf("/?ctrl=ListNews") != -1) {
							perpage = 5;
						}
						CCanvas.s_Instan.connect.setURL(item[indexFocus].Link + "&page="+ NumberPage + "&perpage=" + perpage);
						String linktemp = (String) CCanvas.s_Instan.connect.URL.lastElement();
						if (!linktemp.trim().equalsIgnoreCase(item[indexFocus].Link + "&page=" + NumberPage+ "&perpage=" + perpage)) {
							CCanvas.s_Instan.connect.URL.addElement(item[indexFocus].Link + "&page=" + NumberPage + "&perpage=" + perpage);
							if (!CCanvas.s_Instan.connect.Link .contains(item[indexFocus].Name))
								CCanvas.s_Instan.connect.Link.addElement(item[indexFocus].Name);
						}
						CCanvas.s_Instan.connect.RequestServerGetData();
					} else if (Page == 3) {
						CCanvas.s_Instan.connect.koclearurl = -1;
						CCanvas.s_Instan.LoadingState.timeForLoading = 50;
						CCanvas.s_Instan.LoadingState.time = CCanvas.s_Instan.LoadingState.fixtime;
						CCanvas.CurrentScreen = CCanvas.isChangeLoadingState;
						CCanvas.s_Instan.connect.setURL(item[indexFocus].Link);
						String linktemp = (String) CCanvas.s_Instan.connect.URL.lastElement();
						if (!linktemp.trim().equalsIgnoreCase(item[indexFocus].Link + "&page=" + NumberPage + "&perpage=" + perpage)) {
							CCanvas.s_Instan.connect.URL.addElement(item[indexFocus].Link+ "&page=" + NumberPage + "&perpage=" + perpage);
							if (!CCanvas.s_Instan.connect.Link.contains(item[indexFocus].Name))
								CCanvas.s_Instan.connect.Link.addElement(item[indexFocus].Name);
						}
						CCanvas.s_Instan.connect.RequestServerGetData();

					} else if (Page == 4 || Page == 6) {
						CCanvas.s_Instan.connect.koclearurl = 1;
						CCanvas.s_Instan.LoadingState.timeForLoading = 50;
						CCanvas.s_Instan.LoadingState.time = CCanvas.s_Instan.LoadingState.fixtime;
						CCanvas.CurrentScreen = CCanvas.isChangeLoadingState;
						CCanvas.s_Instan.connect.setURL(item[indexFocus].Link);
						if (item[indexFocus].Link.indexOf("demo=") > 0) {
							CCanvas.s_Instan.connect.URL.addElement(item[indexFocus].Link);
							if (!CCanvas.s_Instan.connect.Link.contains(item[indexFocus].Name))
								CCanvas.s_Instan.connect.Link.addElement(item[indexFocus].Name);
						}
						CCanvas.s_Instan.connect.RequestServerGetData();
					}
				}
			} else if (keyPressed[KeyConstant.KEY_LEFT_SOFTKEY]) {
				CCanvas.submenu.initCommand();
				if (CCanvas.submenu.leftSoftKey != null) {
					CCanvas.submenu.leftSoftKey.action.perform();
				}
				CCanvas.CurrentScreen = CCanvas.isSubMenu;
			} else if (keyPressed[KeyConstant.KEY_RIGHT_SOFTKEY]) {
				CCanvas.s_Instan.LoadingState.timeForLoading = 50;
				CCanvas.s_Instan.LoadingState.time = CCanvas.s_Instan.LoadingState.fixtime;
				CCanvas.CurrentScreen = CCanvas.isChangeLoadingState;

				if (CCanvas.s_Instan.connect.URL.size() > 0) {
					CCanvas.s_Instan.connect.URL
							.removeElementAt(CCanvas.s_Instan.connect.URL
									.size() - 1);
					CCanvas.s_Instan.connect
							.setURL((String) CCanvas.s_Instan.connect.URL
									.lastElement());
				}

				// //////////////////////
				if (NumberPage == 1) {
					if (CCanvas.s_Instan.connect.Link.size() > 0)
						CCanvas.s_Instan.connect.Link
								.removeElementAt(CCanvas.s_Instan.connect.Link
										.size() - 1);
				}
				// ////////////////
				String link = (String) CCanvas.s_Instan.connect.URL
						.lastElement();
				int begin = link.indexOf("&page=");
				if (begin > 0) {
					int iBeginPerpage = link.indexOf("&perpage");
					NumberPage = Integer.parseInt(link.substring(begin + 6,
							iBeginPerpage).trim());
					CCanvas.s_Instan.connect.numberpage = NumberPage;
				}
				CCanvas.s_Instan.connect.RequestServerGetData();
				resetData();
			}
		}
		CCanvas.resetkey(); // User KeyReleased
	}

	public void resetData() {
		indexFocus = 0;
		Cry = 0;
	}

	public void update() {
		ScrollItem();
	}

	public void SwapFocus(int Huong) {
		if (item[indexFocus].isFocus == true) {
			item[indexFocus].isFocus = false;
			item[indexFocus].indexcolor = 0;
			item[indexFocus].indexborder = 0;
		}
		if (PressOnce == false) {
			if (Huong == 0) // up
			{
				if (indexFocus > 0) {
					indexFocus--;
					DetectScrollItem(Huong);
				}
			} else if (Huong == 1) // down
			{
				if (indexFocus < item.length - 1) {
					indexFocus++;
					DetectScrollItem(Huong);
				}
			}
			PressOnce = true;
		}
		item[indexFocus].isFocus = true;
		item[indexFocus].indexcolor = 1;
		item[indexFocus].indexborder = 1;
	}

	public void DetectScrollItem(int UpDown) {
		if (UpDown == 0) // Up
		{
			if ((item[indexFocus].y < ystartLine && indexFocus == 0) || (item[indexFocus].y < ystartLine + item[indexFocus].h * 2
							&& indexFocus != 0 && item[0].y < ystartLine)
					&& Cry == 0) {
				Cry += distanceLine;
				AmDuong = -1;
			}
		} else // Down
		{
			if ((indexFocus < item.length - 1 && item[indexFocus].y + item[indexFocus].h * 2 > LowerLimit -DistanceRecordinPage && item[elementItem - 1].y > LowerLimit - DistanceRecordinPage) && Cry == 0) // +30
			{
				Cry += distanceLine;
				AmDuong = 1;
			}
		}
	}

	public void ScrollItem() {
		if (Cry > 0) {
			for (int i = 0; i < elementItem; i++) {
				item[i].y -= (Cry / NumberDivided + Cry % NumberDivided)* AmDuong;
				item[i].yDescription -= (Cry / NumberDivided + Cry% NumberDivided)* AmDuong;
				item[i].yDate -= (Cry / NumberDivided + Cry % NumberDivided)* AmDuong;
				item[i].yLine -= (Cry / NumberDivided + Cry % NumberDivided)* AmDuong;
				item[i].yPrice -= (Cry / NumberDivided + Cry % NumberDivided)* AmDuong;
			}
			Cry -= (Cry / NumberDivided + Cry % NumberDivided);
		} else {
			Cry = 0;
			PressOnce = false;
		}
	}
}
